<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item  prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item  prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入用户手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['user:member:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['user:member:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['user:member:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['user:member:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column label="用户ID" align="center" prop="id" />
      <el-table-column label="用户名称" align="center" prop="username" />
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          {{ scope.row.gender === 1? '男' : scope.row.gender === 2? '女' : '未知' }}
        </template>
      </el-table-column>

      <el-table-column label="生日" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户层级" align="center" prop="userLevel" >
        <template slot-scope="scope">
          {{ scope.row.userLevel === 0? '普通用户' : scope.row.userLevel === 1? 'VIP用户' : '区域代理用户' }}
        </template>
      </el-table-column>

      <el-table-column label="用户手机号码" align="center" prop="mobile" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          {{ scope.row.status === 0? '可用' : scope.row.status === 1? '禁用' : scope.row.status === 2? '注销' : '代理申请' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" >
              <el-button class="btn" v-if="scope.row.status === 0 && scope.row.userLevel === 0"  type="info">非代理</el-button>
              <el-button class="btn" v-if="scope.row.status === 0 && scope.row.userLevel === 2" @click="tuiguang" type="primary">推广代理</el-button>
              <el-button class="btn" v-if="scope.row.status > 2" @click="handleAdd(scope.row)"  type="primary">审批</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['user:member:edit']"
          >修改</el-button> -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['user:member:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 推广代理 -->
    <el-dialog :title="title" :visible.sync="isDai" width="500px" append-to-body>
      <el-descriptions title="代理详情">
          <el-descriptions-item label="佣金比例(%)"></el-descriptions-item>
      </el-descriptions>
      <el-descriptions>
          <el-descriptions-item label="推广二维码"></el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listMember, getMember, delMember, addMember, updateMember } from "@/api/clientuser/user/member";

export default {
  name: "Member",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员管理表格数据
      memberList: [],
      // 推广代理
      isDai: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: null,
        mobile: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        username: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "用户密码不能为空", trigger: "blur" }
        ],
        gender: [
          { required: true, message: "性别：0 未知， 1男， 1 女不能为空", trigger: "blur" }
        ],
        lastLoginIp: [
          { required: true, message: "最近一次登录IP地址不能为空", trigger: "blur" }
        ],
        nickname: [
          { required: true, message: "用户昵称或网络名称不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "用户手机号码不能为空", trigger: "blur" }
        ],
        avatar: [
          { required: true, message: "用户头像图片不能为空", trigger: "blur" }
        ],
        weixinOpenid: [
          { required: true, message: "微信登录openid不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "0 可用, 1 禁用, 2 注销不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员管理列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    tuiguang(row) {
      this.isDai = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        username: null,
        password: null,
        gender: null,
        birthday: null,
        lastLoginTime: null,
        lastLoginIp: null,
        userLevel: null,
        nickname: null,
        mobile: null,
        avatar: null,
        weixinOpenid: null,
        status: null,
        addTime: null,
        updateTime: null,
        deleted: null,
        shareUserId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 代理审批 */
    handleAdd(row) {
      console.log(JSON.stringify(row, null, 2));
      this.reset();
      const id = row.id || this.ids;
      this.form.id = id;
      this.open = true;
      this.title = "代理审批";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMember(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.userLevel = 2;
            this.form.status = 0;
            updateMember(this.form).then(response => {
              this.$modal.msgSuccess("审批通过");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员管理编号为"' + ids + '"的数据项？').then(function() {
        return delMember(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('clientuser/user/member/export', {
        ...this.queryParams
      }, `member_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
  .btn {
    width: 80px; /* 自定义宽度 */
    height: 40px;
    padding: 0; /* 可以根据需要调整内边距 */
    box-sizing: border-box; /* 确保宽度包含内边距和边框 */
  }
</style>
